/**
 * 
 */
package br.org.bertol.mestrado.engine.optimisation.moo;

import java.util.EnumMap;

import br.org.bertol.mestrado.engine.Objective;

/**
 * @author contaqualquer
 */
public class ParetoMinMinDominace extends
        ParetoDominace<EnumMap<Objective, Number>> {

    @Override
    public final int compare(final EnumMap<Objective, Number> left,
            final EnumMap<Objective, Number> right) {

        int less = 0, bigger = 0, equal = 0;

        final Objective[] objectives = left.keySet()
                .toArray(new Objective[] {});

        if (objectives.length == 1) {

            final int leftV = left.get(objectives[0]).intValue();

            final int rightV = right.get(objectives[0]).intValue();

            if (leftV < rightV) {
                return RIGHT_DOMINE;
            }
            if (rightV > leftV) {
                return LEFT_DOMINE;
            }

            return LEFT_DONT_DOMINE;
        }

        // caso ambos sejam iguais
        // retorna n�o domin�ncia

        // caso esquerda domine direita
        for (Objective objective : objectives) {

            final int leftV = left.get(objective).intValue();

            final int rightV = right.get(objective).intValue();

            // verifica se este objetivo � maior que a refer�ncia
            if (rightV < leftV) {
                bigger += 1;
            } else if (leftV < rightV) {
                less += 1;
            } else {
                equal += 1;
            }
        }

        // caso encerre o la�o e o flag seja true
        // ent�o left n�o � pior em nenhuma solu��o e � melhor em pelo menos uma
        if (less > 0 && equal > 0) {
            return LEFT_DOMINE;
        }
        if (bigger > 0 && equal > 0) {
            return RIGHT_DOMINE;
        }
        if (bigger == left.keySet().size()) {
            return RIGHT_DOMINE;
        }
        if (less == left.keySet().size()) {
            return LEFT_DOMINE;
        }

        // caso contr�rio, left n�o domina right
        return LEFT_DONT_DOMINE;
    }
}
